<?php
require('../include/init.inc.php');
$file = $_FILES["productupfile"];

$max_size = "2000000"; //最大文件限制（单位：byte）
$fname = $file['name'];

$ftype = strtolower(substr(strrchr($fname, '.'), 1));
//文件格式
$uploadfile = $file['tmp_name'];
if (Common::isPost()) {
    if (is_uploaded_file($uploadfile)) {
        if ($file['size'] > $max_size) {
            OSAdmin::alert("error", "导入文件太大");
        }
        if ($ftype != 'xls') {
            OSAdmin::alert("error", "文件类型错误");
        }
    } else {
        OSAdmin::alert("error", "文件为空");
    }
}


$file_arr = ExcelReader::readXLS($uploadfile);
unset($file_arr[0]);

//插入数据库
$ret = AdminProducts::replace($file_arr);

if ($ret) {
    SysLog::addLog(UserSession::getUserName(), 'Update', 'SmallImagesAdd', $products_model, json_encode($up_data));
    OSAdmin::alert("success", "导入成功");
} else {
    OSAdmin::alert("error", "数据库出错");
}

Template::display('one_page_shop/products.tpl');